package com.note.user.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.note.common.dto.UserDto;
import com.note.common.enums.ResponseEnum;
import com.note.common.vo.ResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Package：com.note.user.config
 * @Name：CustomBlockHandler
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2024-04-30-8:30
 * @Description：自定义Sentinel拦截层
 */
@Component
public class CustomBlockHandler {
    /**
     * 限流返回
     * @param username
     * @param exception
     * @return
     */
    @ResponseBody
    public static ResponseVo blockHandleException(@RequestParam String username,BlockException exception) {
        return ResponseVo.fail(ResponseEnum.TOO_MANY_REQUESTS,null);
    }

    /**
     * 熔断设置
     * @param userDto
     * @param throwable
     * @return
     */
    @ResponseBody
    public static ResponseVo fallBackHandle(@RequestBody UserDto userDto,Throwable throwable){
        return ResponseVo.fail(ResponseEnum.CIRCUIT_BREAKER,null);
    }

    /**
     * 热点规则
     * @param userDto
     * @param exception
     * @return
     */
    @ResponseBody
    public static ResponseVo hotBackHandle(@RequestBody UserDto userDto,BlockException exception){
        return ResponseVo.fail(ResponseEnum.HOT_SPOT,null);
    }
}
